package qf.controller;

import qf.dao.daoImpl.UserDaoImpl;
import qf.pojo.User;
import qf.service.serviceImpl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;

/**
 * @author 86198
 * @version V1.0
 * @Project Java_qf
 * @Package qf.controller
 * @Description:
 * @Date 2022/5/27 13:02
 */
@WebServlet("/PersonnelFile")
public class PersonnelFileController extends aa{
    public String login(HttpServletRequest request, HttpServletResponse response){
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //获取前端用户输入的 用户名 与 密码
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        UserDaoImpl userDao = new UserDaoImpl();
        UserServiceImpl userService = new UserServiceImpl(userDao);

        User user = userService.login(username, password);
        request.getSession().setAttribute("user",user);
        if (user != null) {
            request.setAttribute("username",user.getUsername());
            return "redirect:" +request.getContextPath()+ "/Record?method=showAll";
        }else {
            return "redirect:" +request.getContextPath()+ "/login.jsp";
        }
    }
    public String register(HttpServletRequest request, HttpServletResponse response){
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        UserDaoImpl userDao = new UserDaoImpl();
        UserServiceImpl userService = new UserServiceImpl(userDao);

        boolean registered = userService.registered(username, password);
        if (registered) {
            return "redirect:" +request.getContextPath()+ "/login.jsp";
        }else {
            return "redirect:" +request.getContextPath()+ "/register.jsp";
        }
    }
}
